<template>
	<view class="content">
		<view class="top" @click="open">
			<view class="top-name">{{ changeName }}</view>
			<view class="top-icon"><uv-icon name="arrow-down" color="#333"></uv-icon></view>
		</view>
		<view><uv-action-sheet ref="actionSheet" :actions="subjectList" title="选择科目" @select="select" @close="close"></uv-action-sheet></view>
		<view class="list" v-for="item in goexaminationList" >
			<view class="list-left">
				<view class="title">{{ item.questionBankName }}</view>
				<view class="name">
					<view class="">
						试题数量
						<text>105</text>
					</view>
					<view class="">
						做题人数
						<text>8</text>
					</view>
				</view>
			</view>
			<view class="list-right">
				<view class="start"  @click="goexamination(item)">开始练习</view>
				<!-- <view class="start">
					继续练习
				</view>
				<view class="start" style="background-color: #000;">
					查看结果
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
import { searchPlugsPracticeQuestionBankList, getCommonPayInfoByDataId, insertPlugsPracticeStudentTestRecord, getPracticeStudentTestCount,searchPlugsPracticeStudentById,createOrder } from '../../utils/api.js';
export default {
	data() {
		return {
			goexaminationList: [],
			dataId: '',
			changeName: '',
			info: {},
			subjectList: []
		};
	},
	onLoad(option) {
		this.info = JSON.parse(decodeURIComponent(option.info));
		this.changeName = this.info.subjectName;
		this.dataId = this.info.id;

		getPracticeStudentTestCount({ subjectId: this.dataId }).then(res => {});

		// getCommonPayInfoByDataId({ dataId: this.dataId }).then(res => {
		// 	let cont = '当前科目可以免费练习20道题，20道题后收费';
		// 	uni.showModal({
		// 		title: '提示',
		// 		content: cont,
		// 		cancelText: '取消练习',
		// 		cancelColor: '#999',
		// 		confirmText: '去支付',
		// 		success: resp => {},
		// 		fail: () => {}
		// 	});
		// });
	},
	onShow() {
		this.getbankList();
		this.getsubjectlist();
	},
	methods: {
		open() {
			this.$refs.actionSheet.open();
		},
		select(e) {
			console.log( e.subjectName);
			this.changeName = e.subjectName;
			console.log(this.changeName);
			this.dataId = e.id;
			this.getbankList();
		},
		close() {
			console.log('关闭');
		},

		getsubjectlist() {
			let data = {
				id: uni.getStorageSync('studentInfo').id
			};
			searchPlugsPracticeStudentById(data).then(ress => {
				this.subjectList = ress.body.practiceSubjectList;
				this.subjectList.forEach(item=>{
					item.name=item.subjectName
				})
				console.log(this.subjectList);
			});
		},
		getbankList() {
			let data = {
				size: 50,
				page: 1,
				subjectId: this.dataId,
				userServiceToken: 'TUW8JF4WE6D52SDL5UMN5'
			};
			searchPlugsPracticeQuestionBankList(data).then(res => {
				if (res.code == 200) {
					this.goexaminationList = res.body.list;
				}
			});
		},
		goexamination(item) {
			let that=this
			getPracticeStudentTestCount({ subjectId: that.dataId }).then(res => {
				if(res.body>=20){
					getCommonPayInfoByDataId({ dataId: that.dataId,payType: 5 }).
					then(ress => {
						if(!ress.body){
							let cont = '您好，您的20题体验已完成，请付费解锁全部题目';
							uni.showModal({
								title: '提示',
								content: cont,
								cancelText: '取消练习',
								cancelColor: '#999',
								confirmText: '付费',
								success: resp => {
									if(resp.confirm){
										let postData={
											orderType: 5,
											relevanceId: that.dataId,
											logisticsType: 6,
											userId: uni.getStorageSync('userInfo').id,
											payMoney: '',
											cityCode:'',
											expectTime: '',
											bussinessId: '0',
											reservedMobile: '',
											shareUserId:uni.getStorageSync('shareUserId') || -1,
											orderGoodsDTOS: [
												{
													goodsId: 0,
													goodsName: '0',
													goodsNum: 1,
													goodsSpecificationIds: '0',
													goodsSpecificationNames: '0'
												}
											],
											
											shopCartIds: [],
											extJSON: `{subjectId:${that.dataId}}`
										}
										uni.showLoading({
											title: '正在创建订单中'
										});
										createOrder(postData).then((slut)=>{
											if(slut.code==200){
												uni.showToast({
													title: '订单创建成功'
												});
												uni.navigateTo({
													url: `/pages/money/pay?dataId=${that.dataId}&info=${encodeURIComponent(JSON.stringify(slut.body))}`
												});
											}else{
												uni.showToast({
													title: '订单创建失败'
												});
											}
										})
										
									}
								},
								fail: () => {}
							});
						}else{
							let data = {
								questionBankId: item.id,
								startTestTime: '',
								studentId: uni.getStorageSync('studentInfo').id,
								studentName: uni.getStorageSync('studentInfo').studentName,
								testNo: '',
								testStatus: 0,
								adminId: uni.getStorageSync('userInfo').adminId || 47,
								userServiceToken: uni.getStorageSync('userInfo').userServiceToken || 'TUW8JF4WE6D52SDL5UMN5'
							};
												
							insertPlugsPracticeStudentTestRecord(data).then(resss => {
								if (ress.code == 200) {
									uni.navigateTo({
										url: `/pages/examinationList/examination/examination?info=${encodeURIComponent(JSON.stringify(item))}&typeId=0&practiceId=${resss.body.id}`
									});
								}
							});
							// let cont = '开放练习:选择答案后，立即出结果，不进行错题统计！<br>闭卷练习：模拟真实考试环境，需要交卷统计错题!';
							// let content = cont.replace(/<br>/g, '\n');
							// uni.showModal({
							// 	title: '选择练习模式',
							// 	content: content,
							
							// 	cancelText: '开放练习',
							// 	cancelColor: '#999',
							// 	confirmText: '闭卷练习',
							// 	success: resp => {
							// 		console.log(resp);
							// 		if (resp.confirm) {
							// 			uni.navigateTo({
							// 				url: `/pages/examinationList/examination/examination?info=${encodeURIComponent(JSON.stringify(item))}&typeId=1`
							// 			});
							// 		} else if (resp.cancel) {
							// 			console.log(item);
							// 			let data = {
							// 				questionBankId: item.id,
							// 				startTestTime: '',
							// 				studentId: uni.getStorageSync('studentInfo').id,
							// 				studentName: uni.getStorageSync('studentInfo').studentName,
							// 				testNo: '',
							// 				testStatus: 0,
							// 				adminId: uni.getStorageSync('userInfo').adminId || 47,
							// 				userServiceToken: uni.getStorageSync('userInfo').userServiceToken || 'TUW8JF4WE6D52SDL5UMN5'
							// 			};
							
							// 			insertPlugsPracticeStudentTestRecord(data).then(ress => {
							// 				if (ress.code == 200) {
							// 					uni.navigateTo({
							// 						url: `/pages/examinationList/examination/examination?info=${encodeURIComponent(JSON.stringify(item))}&typeId=0&practiceId=${ress.body.id}`
							// 					});
							// 				}
							// 			});
							// 			// uni.navigateTo({
							// 			// 	url: `/pages/examinationList/examination/examination?info=${encodeURIComponent(JSON.stringify(item))}&typeId=0`
							// 			// });
							// 		}
							// 	},
							// 	fail: () => {}
							// });
						}
						
					});
				}else{
					let cont = '当前科目可以体验20道题，后续需要付费';
					uni.showModal({
						title: '提示',
						content: cont,
						cancelText: '取消练习',
						cancelColor: '#999',
						confirmText: '去练习',
						success: resp => {
							if (resp.confirm) {
								let data = {
									questionBankId: item.id,
									startTestTime: '',
									studentId: uni.getStorageSync('studentInfo').id,
									studentName: uni.getStorageSync('studentInfo').studentName,
									testNo: '',
									testStatus: 0,
									adminId: uni.getStorageSync('userInfo').adminId || 47,
									userServiceToken: uni.getStorageSync('userInfo').userServiceToken || 'TUW8JF4WE6D52SDL5UMN5'
								};
													
								insertPlugsPracticeStudentTestRecord(data).then(ress => {
									if (ress.code == 200) {
										uni.navigateTo({
											url: `/pages/examinationList/examination/examination?info=${encodeURIComponent(JSON.stringify(item))}&typeId=0&practiceId=${ress.body.id}`
										});
									}
								});
							}
						},
						fail: () => {}
					});
					}
				// 	let cont = '开放练习:选择答案后，立即出结果，不进行错题统计！<br>闭卷练习：模拟真实考试环境，需要交卷统计错题!';
				// 	let content = cont.replace(/<br>/g, '\n');
				// 	uni.showModal({
				// 		title: '选择练习模式',
				// 		content: content,
					
				// 		cancelText: '开放练习',
				// 		cancelColor: '#999',
				// 		confirmText: '闭卷联系',
				// 		success: resp => {
				// 			console.log(resp);
				// 			if (resp.confirm) {
				// 				uni.navigateTo({
				// 					url: `/pages/examinationList/examination/examination?info=${encodeURIComponent(JSON.stringify(item))}&typeId=1`
				// 				});
				// 			} else if (resp.cancel) {
				// 				let data = {
				// 					questionBankId: item.id,
				// 					startTestTime: '',
				// 					studentId: uni.getStorageSync('studentInfo').id,
				// 					studentName: uni.getStorageSync('studentInfo').studentName,
				// 					testNo: '',
				// 					testStatus: 0,
				// 					adminId: uni.getStorageSync('userInfo').adminId || 47,
				// 					userServiceToken: uni.getStorageSync('userInfo').userServiceToken || 'TUW8JF4WE6D52SDL5UMN5'
				// 				};
					
				// 				insertPlugsPracticeStudentTestRecord(data).then(ress => {
				// 					if (ress.code == 200) {
				// 						uni.navigateTo({
				// 							url: `/pages/examinationList/examination/examination?info=${encodeURIComponent(JSON.stringify(item))}&typeId=0&practiceId=${ress.body.id}`
				// 						});
				// 					}
				// 				});
				// 				// uni.navigateTo({
				// 				// 	url: `/pages/examinationList/examination/examination?info=${encodeURIComponent(JSON.stringify(item))}&typeId=0`
				// 				// });
				// 			}
				// 		},
				// 		fail: () => {}
				// 	});
				// }
			})
			
			// let cont = '开放练习:选择答案后，立即出结果，不进行错题统计！<br>闭卷练习：模拟真实考试环境，需要交卷统计错题!';
			// let content = cont.replace(/<br>/g, '\n');
			// uni.showModal({
			// 	title: '选择练习模式',
			// 	content: content,

			// 	cancelText: '开放练习',
			// 	cancelColor: '#999',
			// 	confirmText: '闭卷联系',
			// 	success: resp => {
			// 		console.log(resp);
			// 		if (resp.confirm) {
			// 			uni.navigateTo({
			// 				url: `/pages/examinationList/examination/examination?info=${encodeURIComponent(JSON.stringify(item))}&typeId=1`
			// 			});
			// 		} else if (resp.cancel) {
			// 			let data = {
			// 				questionBankId: item.id,
			// 				startTestTime: '',
			// 				studentId: uni.getStorageSync('studentInfo').id,
			// 				studentName: uni.getStorageSync('studentInfo').studentName,
			// 				testNo: '',
			// 				testStatus: 0,
			// 				adminId: uni.getStorageSync('userInfo').adminId || 47,
			// 				userServiceToken: uni.getStorageSync('userInfo').userServiceToken || 'TUW8JF4WE6D52SDL5UMN5'
			// 			};

			// 			insertPlugsPracticeStudentTestRecord(data).then(ress => {
			// 				if (ress.code == 200) {
			// 					uni.navigateTo({
			// 						url: `/pages/examinationList/examination/examination?info=${encodeURIComponent(JSON.stringify(item))}&typeId=0&practiceId=${ress.body.id}`
			// 					});
			// 				}
			// 			});
			// 			// uni.navigateTo({
			// 			// 	url: `/pages/examinationList/examination/examination?info=${encodeURIComponent(JSON.stringify(item))}&typeId=0`
			// 			// });
			// 		}
			// 	},
			// 	fail: () => {}
			// });
		},
		
	}
};
</script>

<style lang="scss">
uni-modal .uni-modal__bd {
	white-space: pre-wrap;
}
page {
	padding: 20rpx;
	box-sizing: border-box;
}
.top {
	display: flex;
	align-items: center;
	background-color: #f5f5f5;
	padding: 20rpx;
	margin-bottom: 20rpx;
	.top-name {
		color: $uni-text-color;
		font-size: 26rpx;
	}
	.top-icon {
	}
}
.content {
	.list {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx;
		border-bottom: 1rpx solid #f5f5f5;
		.list-left {
			.title {
				color: $uni-text-color;
				font-weight: 700;
				font-size: 28rpx;
			}
			.name {
				width: 450rpx;
				display: flex;
				align-items: center;
				padding: 10rpx 0;
				justify-content: space-between;
				font-size: 24rpx;
				color: #666;
				text {
					margin-left: 10rpx;
				}
			}
		}
		.list-right {
			.start {
				font-size: 26rpx;
				color: #fff;
				background-color: royalblue;
				width: 140rpx;
				height: 50rpx;
				line-height: 50rpx;
				text-align: center;
				border-radius: 25rpx 25rpx 0 25rpx;
			}
		}
	}
}
</style>
